/**
 * Project Name:fire-industry-DM-center
 * File Name:TransUtil.java
 * Package Name:com.firestone.core.utils
 * Date:2018年4月16日下午5:09:24
 *
*/

package com.firestone.core.utils;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.firestone.bean.entity.elasticsearch.im.BaseEntity;
import com.firestone.bean.entity.elasticsearch.nested.Category;
import com.firestone.bean.entity.elasticsearch.nested.Record;
import com.firestone.core.constants.ConstantKey;
import com.firestone.core.enums.SystemCodeEnum;
import com.firestone.core.exception.FireException;

/**
 * @Description: TODO(ES bean转换处理)
 * @author CAIYJ
 * @date 2018年4月16日 下午5:09:24
 */
public class TransUtil {

    private static Pattern linePattern = Pattern.compile("_(\\w)");

    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    /**
     * @Description: TODO(企业信息转换，存在修改记录的字段不转换)
     * @param dmMapProject
     * @param dmImProject
     * @return
     * @author CAIYJ
     * @date 2018年4月16日 下午2:21:04
     * @lastModifier CAIYJ
     */
    public static <T> void fieldTransExcludeRecord(BaseEntity source,
            BaseEntity target, Class<T> clazz) {
        if (null != source && null != target && null != source.getRecords()
                && source.getRecords().size() > 0) {
            for (Record record : source.getRecords()) {
                for (Field field : clazz.getDeclaredFields()) {
                    if (record.getField().equals(field.getName())) {
                        try {
                            field.setAccessible(true);
                            field.set(target, field.get(source));
                        } catch (Exception e) {
                            throw new FireException(
                                    SystemCodeEnum.http_status_internal_server_error
                                            .getErrorCode(),
                                    "反射获取值异常", e);
                        }
                        break;
                    }
                }
            }
        }
    }

    /**
     * @Description: TODO(企业信息转换，category转换,企业区域上线)
     * @param source
     * @param target
     * @param mapIds
     * @author CAIYJ
     * @date 2018年4月16日 下午4:49:32
     * @lastModifier CAIYJ
     */
    public static void categoryTransUp(BaseEntity source, BaseEntity target,
            Long... mapIds) {
        Set<Category> categorySet = new HashSet<>();
        if (null != mapIds && mapIds.length > 0) {
            Category category = null;
            for (Long mapId : mapIds) {
                category = new Category();
                category.setMapId(mapId.toString());
                category.setStatus(ConstantKey.ES_CATEGORY_STATUS_USE);
                categorySet.add(category);
            }
            if (null != source && null != source.getCategory()) {
                out: for (Iterator<Category> ite = source.getCategory()
                        .iterator(); ite.hasNext();) {
                    if (null != mapIds && mapIds.length > 0) {
                        for (Long mapId : mapIds) {
                            if (mapId.toString()
                                    .equals(ite.next().getMapId())) {
                                ite.remove();
                                continue out;
                            }
                        }
                    }
                }
                categorySet.addAll(source.getCategory());
            }
        }
        target.setCategory(categorySet);
    }

    /**
     * @Description: TODO(企业信息转换，category转换，企业区域下线)
     * @param source
     * @param target
     * @param mapIds
     * @author CAIYJ
     * @date 2018年4月16日 下午5:33:13
     * @lastModifier CAIYJ
     */
    public static void categoryTransDown(BaseEntity source, Long... mapIds) {
        if (null != source && null != source.getCategory()
                && source.getCategory().size() > 0) {
            out: for (Iterator<Category> ite = source.getCategory()
                    .iterator(); ite.hasNext();) {
                if (null != mapIds && mapIds.length > 0) {
                    for (Long mapId : mapIds) {
                        if (mapId.toString().equals(ite.next().getMapId())) {
                            ite.remove();
                            continue out;
                        }
                    }
                }
            }
        }
    }

    /**
     * @Description: TODO(拼接keyword类型的正则表达查询式)
     * @param param
     * @return
     * @author CAIYJ
     * @date 2018年4月12日 下午4:45:50
     * @lastModifier CAIYJ
     */
    public static String queryStringTrans(String param) {
        StringBuffer sb = new StringBuffer();
        sb.append(ConstantKey.ASTERISK);
        sb.append(param);
        sb.append(ConstantKey.ASTERISK);
        return sb.toString();
    }

    /**
     * @Description: TODO(下划线转驼峰)
     * @param str
     * @return
     * @author CAIYJ
     * @date 2018年4月20日 上午10:08:45
     * @lastModifier CAIYJ
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * @Description: TODO(驼峰转下划线)
     * @param str
     * @return
     * @author CAIYJ
     * @date 2018年4月20日 上午10:09:27
     * @lastModifier CAIYJ
     */
    public static String humpToLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
